/**
 * @author Administrator
 */
Ext.data.FrameProxy = function(o){
    Ext.data.FrameProxy.superclass.constructor.call(this);
    o=o||{};	
		this.serviceName = o.serviceName;
		this.doc=o.doc;
};

Ext.extend(Ext.data.FrameProxy, Ext.data.DataProxy, {

    /**
     * Load data from the configured {@link Ext.data.Connection}, read the data object into
     * a block of Ext.data.Records using the passed {@link Ext.data.DataReader} implementation, and
     * process that block using the passed callback.
     * @param {Object} params An object containing properties which are to be used as HTTP parameters
     * for the request to the remote server.
     * @param {Ext.data.DataReader} reader The Reader object which converts the data
     * object into a block of Ext.data.Records.
     * @param {Function} callback The function into which to pass the block of Ext.data.Records.
     * The function must be passed <ul>
     * <li>The Record block object</li>
     * <li>The "arg" argument from the load function</li>
     * <li>A boolean success indicator</li>
     * </ul>
     * @param {Object} scope The scope in which to call the callback
     * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
     */
    load : function(params, reader, callback, scope, arg){
        if(this.fireEvent("beforeload", this, params) !== false){
						var  o = {
                params : params || {},
                request: {
                    callback : callback,
                    scope : scope,
                    arg : arg
                },
                reader: reader,
                callback : this.loadResponse,
                scope: this
            };
            //debugger;
            var start = params['start'];
            if(this.doc!=null && (this.serviceName==null || start==null)){
            		var result;
				        try {
				            result = o.reader.readRecords(this.doc);
				        }catch(e){
				            this.fireEvent("loadexception", this, o, this.doc, e);
				            o.request.callback.call(o.request.scope, null, o.request.arg, false);
				            return;
				        }
				        this.fireEvent("load", this, o, o.request.arg);
				        o.request.callback.call(o.request.scope, result, o.request.arg, true);
            }else if(this.serviceName!=null){
            		var svc = new Service(this.serviceName);
								argu = params['argu'];								
								var limit = params['limit'];
								var pageindex = 1;
				        if(limit!=null && limit>0){
				        	pageindex += Math.ceil(start/limit);
				        	if(typeof(argu)=="undefined"){
				        		argu="<PAGEINDEX>"+pageindex+"</PAGEINDEX><PAGESIZE>"+limit+"</PAGESIZE>";
				        	}else if(argu=="" || argu.indexOf("<PAGEINDEX>")==-1){
				        		argu+="<PAGEINDEX>"+pageindex+"</PAGEINDEX>";
				        	}
				        	if(argu.indexOf("<PAGESIZE>")==-1){
				        		argu+="<PAGESIZE>"+limit+"</PAGESIZE>";
				        	}
			        		subargu=argu.substring(argu.indexOf("<PAGEINDEX>"),argu.indexOf("</PAGEINDEX>")+"</PAGEINDEX>".length);
			        		argu=argu.replace(subargu,"<PAGEINDEX>"+pageindex+"</PAGEINDEX>");
			        		subargu=argu.substring(argu.indexOf("<PAGESIZE>"),argu.indexOf("</PAGESIZE>")+"</PAGESIZE>".length);
			        		argu=argu.replace(subargu,"<PAGESIZE>"+limit+"</PAGESIZE>");
				        }  
								var response=svc.doService(argu);
								if (svc.getCode()!="2000")
						    {
						        showMessage(svc.getMessage());
				            this.fireEvent("loadexception", this, o, response);
				            o.request.callback.call(o.request.scope, null, o.request.arg, false);
				            return;
						    }else{    
						        var result;
						        try {
						        		var oDoc=loadXml(response);
						            result = o.reader.readRecords(oDoc);
						        }catch(e){
						            this.fireEvent("loadexception", this, o, response, e);
						            o.request.callback.call(o.request.scope, null, o.request.arg, false);
						            return;
						        }
						        this.fireEvent("load", this, o, o.request.arg);
						        o.request.callback.call(o.request.scope, result, o.request.arg, true);
						    }
						}else{
								this.fireEvent("loadexception", this, o, response, e);
		            o.request.callback.call(o.request.scope, null, o.request.arg, false);
		            return;
						}
        }else{
            callback.call(scope||this, null, arg, false);
        }
    },
    
    //private
    setDoc : function(doc){
        this.doc=doc;
    },
    
    //private
    setServiceName : function(serviceName){
        this.serviceName=serviceName;
    }
});
